package com.baway.home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.baway.common.entity.ServiceEntity
import com.baway.home.databinding.HotServiceItemLayoutBinding

/**
 * Author: Jason Zhang
 * Date: 2025/8/28
 * Desc: 热门服务适配器
 */
class HotServiceAdapter(private val data: List<ServiceEntity>): RecyclerView.Adapter<HotServiceAdapter.HotItemViewHolder>(){

    private var itemOnClickListener: ItemOnClickListener? = null

    fun setItemOnClickListener(listener: ItemOnClickListener){
        this.itemOnClickListener = listener
    }

    inner class HotItemViewHolder(val itemDataBinding: HotServiceItemLayoutBinding): RecyclerView.ViewHolder(itemDataBinding.root)

    override fun getItemCount(): Int = data.size

    /**
     * 创建ViewHolder
     */
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HotItemViewHolder {
        return HotItemViewHolder(HotServiceItemLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    /**
     * 绑定数据
     */
    override fun onBindViewHolder(holder: HotItemViewHolder, position: Int) {
        val entity = data[position]

        //方法一：使用DataBinding给UI组件绑定数据
        holder.itemDataBinding.entity = entity
        holder.itemDataBinding.executePendingBindings()

        //方法二：传统方式
        //holder.itemDataBinding.tvServiceName.text = entity.name
        //GlideImgLoader.loadResImage(holder.itemView, entity.resId, holder.itemDataBinding.ivServiceIcon)

        //设置点击事件
        holder.itemDataBinding.root.setOnClickListener {
            itemOnClickListener?.onClick(it, position)
        }

    }

    /**
     * 自定义点击事件接口
     */
    interface ItemOnClickListener{
        fun onClick(v: View, position: Int)
    }


}